package com.oa.service.user.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.oa.mapper.user.TodoMapper;
import com.oa.pojo.TodoList;
import com.oa.service.user.TodoService;
import com.oa.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

/**
 * @Author: 一拳超人
 * @Date: 2021/10/12 15:28
 */
@Service
public class TodoServiceImpl implements TodoService {
    @Autowired
    private TodoMapper todoMapper;

    @Override
    public TodoList addNerTodo(TodoList todoList) {
        todoList.setStartTime(DateUtil.parse(DateUtil.today()));
        todoMapper.insert(todoList);
        return todoMapper.selectById(todoList);
    }

    @Override
    public PageResult getAllTodoList(PageResult pageResult) {
        IPage<TodoList> page = new Page<>(pageResult.getPageNum(), pageResult.getPageSize());
        QueryWrapper<TodoList> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.hasLength(pageResult.getQuery()), "todo_name", pageResult.getQuery())
                .eq("emp_id", pageResult.getEmpId());
        page = todoMapper.selectPage(page, queryWrapper);
        pageResult.setTotal(page.getTotal()).setRows(page.getRecords());
        return pageResult;
    }

    @Override
    public PageResult getGoingTodoList(PageResult pageResult) {
        IPage<TodoList> page = new Page<>(pageResult.getPageNum(), pageResult.getPageSize());
        QueryWrapper<TodoList> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.hasLength(pageResult.getQuery()), "todo_name", pageResult.getQuery())
                .eq("emp_id", pageResult.getEmpId())
                .eq("todo_status", false);
        page = todoMapper.selectPage(page, queryWrapper);
        pageResult.setTotal(page.getTotal()).setRows(page.getRecords());
        return pageResult;
    }

    @Override
    public PageResult getEndTodoList(PageResult pageResult) {
        IPage<TodoList> page = new Page<>(pageResult.getPageNum(), pageResult.getPageSize());
        QueryWrapper<TodoList> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.hasLength(pageResult.getQuery()), "todo_name", pageResult.getQuery())
                .eq("emp_id", pageResult.getEmpId())
                .eq("todo_status", true);
        page = todoMapper.selectPage(page, queryWrapper);
        pageResult.setTotal(page.getTotal()).setRows(page.getRecords());
        return pageResult;
    }

    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public String updateTodoStatus(TodoList todoList) {
        if (!todoList.getTodoStatus()) {
            todoList.setEndTime(null);
        } else {
            todoList.setEndTime(DateUtil.parse(DateUtil.today()));
        }
        int i = todoMapper.updateById(todoList);
        return i != 0 ? "成功" : "失败";
    }

    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public String deleteTodo(TodoList todoList) {
        int i = todoMapper.deleteById(todoList);
        return i != 0 ? "成功" : "失败";
    }

    @Override
    public TodoList getTodoInfo(Integer id) {
        return todoMapper.selectById(id);
    }

    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public String updateTodoInfo(TodoList todoList) {
        int i = todoMapper.updateById(todoList);
        return i != 0 ? "更新成功" : "更新失败";
    }
}
